var url_seach="/web/api/product/productSale/search_productSale";
var url_puton="/web/api/product/productSale/put_on_sale";
var url_pulloff="/web/api/product/productSale/pull_off";
function getUrlParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
	var r = window.location.search.substr(1).match(reg);  //匹配目标参数
	if (r != null) return decodeURI(r[2]); return null; //返回参数值
}

$(function(){
	//1.初始化Table
	var oTable = new TableInit();
	oTable.Init();
})

$("#product_seach_form").validate({
	rules: {
		product_id: {
			required: false,
			number:true
		},
		product_type: {
			required: false,
			number:true
		},
		status:{
			required: false,
			number:true
		}
	},
	messages: {
		product_id: {
			required: "请输入正确的商品ID"
		},
		product_type: {
			required: "请输入正确的商品类型"
		},
		status: {
			required: "请输入正确的状态"
		}
	},
	errorElement:"em"
});

function search() {
	window.location.href = window.location.pathname+"?p=1"+"&product_id="+$.trim($("#product_id").val())+"&product_type="+$.trim($("#product_type option:checked").val())+"&product_name="+$.trim($("#product_name").val());
	//localStorage.product_name=$.trim($("#product_name").val());
	var oTable = new TableInit();
	oTable.Init();
}
var TableInit = function () {
	var oTableInit = new Object();
	oTableInit.Init = function () {
		$("#tbody_list_table").empty();
		$("#product_pc").empty();
		$('#retrieve').empty();
		var pag =  getUrlParam("p");
		if( pag == null || pag==undefined || pag==""){
			pag=1;
		};
		var product_id = getUrlParam("product_id");
		var product_type = getUrlParam("product_type");
		var product_name = getUrlParam("product_name");
		//alert(product_name);
		$.ajax({
			url: producturl + url_seach,
			dataType:'json',
			type:'post',
			xhrFields: {
				withCredentials: true
			},
			crossDomain: true,
			data:{
				"product_id": product_id,
				"product_type": product_type,
				// "status": $.trim($("#status").val()),
				"product_name": product_name,
				"NEED_DO_CHECK": "false",
				"pageNo":pag,//分页返回问题
				"pageSize":20
			},
			beforeSend:function(){
				common.lightBox.showLoading();
				$('#totalpage').css('border','1px solid white');
			},
			complete:function(){
				common.lightBox.hideLoading();
				$('#totalpage').css('border','1px solid #ddd');
			},
			success: function (data) {
				console.log(data);
				$("#product_id").val(product_id);
				$("#product_name").val(product_name);
				$("#product_type option[value='"+product_type+"']").prop("selected","selected");
				if (data != null && data.result.listProductSale.length>0) {
					setTbodyList(data);
					var pc = data.result.pageController; //取到pageCount的值(把返回数据转成object类型)
					var currentPage = pc.currentPage; //得到currentPage
					var pageCount = pc.totalPages; //得到totalPages
					var pageSize = pc.pageSize; //得到totalPages
					var pageTotalRowsAmount = pc.totalRowsAmount//得到总条数
					var options = {
						bootstrapMajorVersion: 3, //版本
						currentPage: currentPage, //当前页数
						totalPages: pageCount, //总页数
						numberOfPages:11,
						itemTexts: function (type, page, current) {
							switch (type) {
								case "first":
									return "首页";
								case "prev":
									return "上一页";
								case "next":
									return "下一页";
								case "last":
									return "末页";
								case "page":
									return page;
							}
						},//点击事件，用于通过Ajax来刷新整个tbody_list_table列表
						onPageClicked: function (event, originalEvent, type, page) {
							// setPageParam(page);
							window.location.href = window.location.pathname+"?p="+page+"&product_id="+$.trim($("#product_id").val())+"&product_type="+$.trim($("#product_type option:checked").val())+"&product_name="+$.trim($("#product_name").val());
							$.ajax({
								url: producturl + url_seach ,
								type: "post",
								xhrFields: {
									withCredentials: true
								},
								crossDomain: true,
								data:{
									"product_id": product_id,
									"product_type": product_type,
									// "status": $.trim($("#status").val()),
									"product_name": product_name,
									"NEED_DO_CHECK": "false",
									"pageNo":page,
									"pageSize":pageSize
								},
								success: function (data1) {
									$("#product_id").val(product_id);
									$("#product_name").val(product_name);
									$("#product_type option[value='"+product_type+"']").prop("selected","selected");
									setTbodyList(data1);
								}
							});
						}
					};
					$('#product_pc').bootstrapPaginator(options);
					$('#totalpage').empty().prepend("<li style='line-height: 34px;'>总条数："+pageTotalRowsAmount+"</li>");
					$('#retrieve').prepend("当前检索条件共有"+pageTotalRowsAmount+"条数据");

				}else{
					setTbodyList(data);
					$('#retrieve').prepend("当前检索条件共有0条数据");
				}
			}
		});
	}
	return oTableInit;
}

function setTbodyList(data){
	if (data != null ) {
		var tablelistPCProduct = new Array();
		$.each(data.result.listProductSale, function (index, item) { //遍历返回的json
			tablelistPCProduct.push('<tr>');
			tablelistPCProduct.push('<td style="display: none">' + (index+1)+ '</td>');
			tablelistPCProduct.push('<td>' + item.id + '</td>');
			// switch(item.product_type){
			// 	case 100:
			// 		tablelistPCProduct.push('<td>虚拟</td>');
			// 		break;
			// 	case 200:
			// 		tablelistPCProduct.push('<td>实物</td>');
			// 		break;
			// 	default:
			// 		tablelistPCProduct.push('<td>未标识</td>');
			// }
			tablelistPCProduct.push('<td>' + item.name + '</td>');

			// switch(item.status){
			// 	case 100:
			// 		tablelistPCProduct.push('<td>待审核</td>');
			// 		break;
			// 	case 200:
			// 		tablelistPCProduct.push('<td>上架</td>');
			// 		break;
			// 	case 300:
			// 		tablelistPCProduct.push('<td>下架</td>');
			// 		break;
			// 	case 400:
			// 		tablelistPCProduct.push('<td>无货</td>');
			// 		break;
			// 	default:
			// 		tablelistPCProduct.push('<td>未标识</td>');
			// }
			var receiveDate = "";
			var receiveTimes = "";
			if(item.three_type == 400 && item.my_attr_json != ""){
				var myAttrJson = eval('(' +item.my_attr_json+ ')');
				if(myAttrJson != null){
					receiveDate  = myAttrJson[0].attr_value;
					receiveTimes = myAttrJson[1].attr_value;
				}
			}
			tablelistPCProduct.push('<td>' + receiveDate + '</td>');
			tablelistPCProduct.push('<td>' + receiveTimes + '</td>');
            tablelistPCProduct.push('<td>' + item.price.toFixed(2) + '</td>');
			tablelistPCProduct.push('<td>');
			tablelistPCProduct.push('<a href="productInfo.html?flag=showInfo&enable=false&product_id='+item.id+'"  class="btn btn-info btn-xs productDetail" style="background-color: #576b95;border-color: #576b95;">查看</a>');
			tablelistPCProduct.push(' &nbsp;&nbsp;&nbsp;');
			tablelistPCProduct.push('<a href="productInfo.html?product_id='+item.id+'" class="btn btn-primary btn-xs edit" style="background-color: #c4d703;border-color: #c4d703;">编辑</a>');
			tablelistPCProduct.push(' &nbsp;&nbsp;&nbsp;');
			// if(item.status == 100 || item.status == 300){
			// 	// tablelistPCProduct.push('<a href="javascript:void(0);" class="btn btn-info btn-xs productDetail" onclick="updateStatus(\''+item.id+'\',200)">上架</a>');
			// }else{
			// 	tablelistPCProduct.push('<a href="javascript:void(0);" class="btn btn-info btn-xs productDetail" onclick="updateStatus(\''+item.id+'\',300)">下架</a>');
			// }
			tablelistPCProduct.push('</td>');
			tablelistPCProduct.push('</tr>');
		});
		if(data.result.listProductSale.length==0){
			$("#totalpage").hide();
			tablelistPCProduct.push( '<tr style="font-size: 20px;font-family:Helvetica Neue, Helvetica, Arial, sans-serif;"><td colspan="8">没有数据</td></tr>');
		}
		$("#tbody_list_table").html(tablelistPCProduct.join(''));
	}
}

//上架下架设置
function updateStatus(id,status) {
	var requesturl;
	if(status == 200){
		requesturl = producturl + url_puton;
	}else{
		requesturl = producturl + url_pulloff;
	}
	$.postRequest(requesturl, {"product_ids":id,"NEED_DO_CHECK": "false"}, function (data) {
		if(data.status == "200"){
			alert("操作成功");
			var oTable = new TableInit();
			oTable.Init();
		}else{
			alert(data.message);
		}
	});
}


